package cn.com.taiji.oospub.wechat.common;

import cn.com.taiji.oospub.wechat.common.tool.NetTools;
import cn.com.taiji.oospub.wechat.common.tool.TimeTools;
import cn.com.taiji.oospub.wechat.common.wxpay.pay.Configure;
import cn.com.taiji.oospub.wechat.entity.*;
import com.google.gson.Gson;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Configuration
@Slf4j
public class OrderSend {
    @Value("${Template_newSend_id}")
    public String Template_newSend_id;
    @Value("${Template_id}")
    public String Template_id;
    //退款通知
    @Value("${Template_tuiKun_id}")
    public String Template_tuiKun_id;
    //预订提醒通知
    @Value("${Template_jieCan_id}")
    public String Template_jieCan_id;
    //取餐通知
    @Value("${Template_quCan_id}")
    public String Template_quCan_id;
    //消费通知
    @Value("${Template_xiaoFeiTongZhi_id}")
    public String Template_xiaoFeiTongZhi_id;
    //订单支付成功通知
    @Value("${Template_dingDangZhifu_id}")
    public String Template_dingDangZhifu_id;
    //京西--余额不足
    @Value("${Template_yuebuzu_id}")
    public String Template_yuebuzu_id;
    @Value("${Template_chuzhi_id}")
    public String Template_chuzhi_id;

    //--------现金支付时消息推送开始-----------
    public JSONObject metXianJin(String access_token, MemberBaseInfo memberBaseInfo, CoursesOrders courOrders,
                                 List<CoursesOrdersDetails> detailList, String orderSendMusic) {
        WechatTemplate wechatTemplate = new WechatTemplate();
        wechatTemplate.setTemplate_id(Template_id);
        wechatTemplate.setTouser(memberBaseInfo.getOpenid());
//        wechatTemplate.setUrl("http://music.163.com/#/song?id=27867140"); 
        wechatTemplate.setUrl(orderSendMusic);
        Map<String, TemplateData> m = new HashMap<String, TemplateData>();
        TemplateData first = new TemplateData();
        first.setColor("#000000");
        first.setValue("亲！我们已收到您(" + memberBaseInfo.getName() + ")的订单，正在精心备餐中，请你取餐时进行付款,您可在我的订单中查看。请记得关注我们呦！^_^");
        m.put("first", first);
        //订单编号
        TemplateData keyword1 = new TemplateData();
        keyword1.setColor("#000000");
        keyword1.setValue(courOrders.getOrderCode());
        m.put("keyword1", keyword1);
        //商家名称
        TemplateData keyword2 = new TemplateData();
        keyword2.setColor("#000000");
        keyword2.setValue(memberBaseInfo.getName());
        m.put("keyword2", keyword2);

        //订单内容
        TemplateData keyword3 = new TemplateData();
        keyword3.setColor("#000000");
        keyword3.setValue(TimeTools.calendar2String(Calendar.getInstance(), TimeTools.DAY_FORMAT));
        m.put("keyword3", keyword3);


        TemplateData remark = new TemplateData();
        remark.setColor("#000000");

        if (detailList != null && detailList.size() > 0 && detailList.get(0).getQucanStartTime() != null && detailList.get(0).getQucanEndTime() != null) {
            remark.setValue("感谢您的点餐！");
        } else {
            remark.setValue("感谢您的点餐！");
        }

        m.put("remark", remark);
        wechatTemplate.setData(m);
        try {
            return sendTemplateMessage(access_token, wechatTemplate);
        } catch (Exception e) {
            log.info("异常" + e.getMessage());
        }
        return null;
    }

    //	订餐餐卡或微信支付
    public JSONObject sendMet(String access_token, MemberBaseInfo memberBaseInfo, String orderCode, String kongxc_wx_orderSendMusic) {
        WechatTemplate wechatTemplate = new WechatTemplate();
        wechatTemplate.setTemplate_id(Template_id);
        wechatTemplate.setTouser(memberBaseInfo.getOpenid());
        if (StringUtils.hasText(kongxc_wx_orderSendMusic)) {
            wechatTemplate.setUrl(kongxc_wx_orderSendMusic);
        }
        Map<String, TemplateData> m = new HashMap<String, TemplateData>();
        TemplateData first = new TemplateData();
        first.setColor("#000000");
        first.setValue("亲！我们已收到您(" + memberBaseInfo.getName() + ")支付的订单金额，正在精心备餐中，您可在我的订单中查看。请记得关注我们呦！^_^");
        m.put("first", first);
        //订单编号
        TemplateData keyword1 = new TemplateData();
        keyword1.setColor("#000000");
        keyword1.setValue(orderCode);
        m.put("keyword1", keyword1);
        //商家名称
        TemplateData keyword2 = new TemplateData();
        keyword2.setColor("#000000");
        keyword2.setValue(memberBaseInfo.getName());
        m.put("keyword2", keyword2);

        //订单内容
        TemplateData keyword3 = new TemplateData();
        keyword3.setColor("#000000");
        keyword3.setValue(TimeTools.calendar2String(Calendar.getInstance(), TimeTools.DAY_FORMAT));
        m.put("keyword3", keyword3);

        TemplateData remark = new TemplateData();
        remark.setColor("#000000");
        remark.setValue("感谢您的点餐,欢迎您下次光临！");
        m.put("remark", remark);
        wechatTemplate.setData(m);
        try {
            return sendTemplateMessage(access_token, wechatTemplate);
        } catch (Exception e) {
            log.info("异常" + e.getMessage());
        }
        return null;
    }

    //	订餐取消通知
    public JSONObject tuiKunMethod(String access_token, MemberBaseInfo memberBaseInfo, CoursesOrders courOrders,
                                   String kongxc_wx_diancan_orderServlet) {
        WechatTemplate wechatTemplate = new WechatTemplate();
        wechatTemplate.setTemplate_id(Template_tuiKun_id);
        wechatTemplate.setTouser(memberBaseInfo.getOpenid());
        if (StringUtils.hasText(kongxc_wx_diancan_orderServlet)) {
            wechatTemplate.setUrl(kongxc_wx_diancan_orderServlet);
        }
        Map<String, TemplateData> m = new HashMap<String, TemplateData>();
        TemplateData first = new TemplateData();
        first.setColor("#000000");
        first.setValue("您已成功申请退款,退款稍后会按原路退回到您的支付账户,请注意查收!");
        m.put("first", first);
        //订单编号
        TemplateData keyword1 = new TemplateData();
        keyword1.setColor("#000000");
        keyword1.setValue(courOrders.getOrderCode());
        m.put("keyword1", keyword1);
        //订单金额
        TemplateData keyword2 = new TemplateData();
        keyword2.setColor("#000000");
        keyword2.setValue(courOrders.getAmount().toString());
        m.put("keyword2", keyword2);
        //会员姓名
        TemplateData keyword3 = new TemplateData();
        keyword3.setColor("#000000");
        keyword3.setValue(memberBaseInfo.getName());
        m.put("keyword3", keyword3);
        //联系电话
        TemplateData keyword4 = new TemplateData();
        keyword4.setColor("#000000");
        keyword4.setValue(memberBaseInfo.getSchool().getMobile());
        m.put("keyword4", keyword4);
        //退款时间
        TemplateData keyword5 = new TemplateData();
        keyword5.setColor("#000000");
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年M月d日 H时m分");
        keyword5.setValue(dateTimeFormatter.format(LocalDateTime.now()));
        m.put("keyword5", keyword5);
        TemplateData remark = new TemplateData();
        remark.setColor("#000000");
        remark.setValue("感谢您的点餐，给您带来的不便我们感到抱歉。如果您对这条消息有任何疑问请联系餐厅管理人员。");
        m.put("remark", remark);
        wechatTemplate.setData(m);
        try {
            return sendTemplateMessage(access_token, wechatTemplate);
        } catch (Exception e) {
            log.info("异常" + e.getMessage());
        }
        return null;
    }


    //	储值成功通知
    public JSONObject chuzhiMethod(String access_token, MemberBaseInfo memberBaseInfo, CoursesOrders courOrders) {
        WechatTemplate wechatTemplate = new WechatTemplate();
        wechatTemplate.setTemplate_id(Template_chuzhi_id);
        wechatTemplate.setTouser(memberBaseInfo.getOpenid());
        Map<String, TemplateData> m = new HashMap<String, TemplateData>();
        TemplateData first = new TemplateData();
        first.setColor("#000000");
        first.setValue("用户 " + memberBaseInfo.getMobile() + " 储值成功。");
        m.put("first", first);
        //储值金额
        TemplateData keyword1 = new TemplateData();
        keyword1.setColor("#000000");
        keyword1.setValue(memberBaseInfo.getIdNumber());
        m.put("keyword1", keyword1);
        //详情
        TemplateData keyword2 = new TemplateData();
        keyword2.setColor("#000000");
        keyword2.setValue(courOrders.getAmount().toString());
        m.put("keyword2", keyword2);
        //操作门店
        TemplateData keyword3 = new TemplateData();
        keyword3.setColor("#000000");
        keyword3.setValue("爱玛客");
        m.put("keyword3", keyword3);
        //可用储值
        TemplateData keyword4 = new TemplateData();
        keyword4.setColor("#000000");
        keyword4.setValue(courOrders.getAfterAmount().toString());
        m.put("keyword4", keyword4);
        //时间
        TemplateData keyword5 = new TemplateData();
        keyword5.setColor("#000000");
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy年M月d日 H时m分");
        keyword5.setValue(dateTimeFormatter.format(LocalDateTime.now()));
        m.put("keyword5", keyword5);
        TemplateData remark = new TemplateData();
        remark.setColor("#000000");
        remark.setValue("感谢您的使用。");
        m.put("remark", remark);
        wechatTemplate.setData(m);
        try {
            return sendTemplateMessage(access_token, wechatTemplate);
        } catch (Exception e) {
            log.info("异常" + e.getMessage());
        }
        return null;
    }


    //	订餐接餐通知
    public JSONObject jieCanMethod(String access_token, MemberBaseInfo memberBaseInfo, CoursesOrders courOrders, List<CoursesOrdersDetails> detailList, String courseDetail, String kongxc_wx_diancan_orderServlet) {
        WechatTemplate wechatTemplate = new WechatTemplate();
        wechatTemplate.setTemplate_id(Template_jieCan_id);
        wechatTemplate.setTouser(memberBaseInfo.getOpenid());
        wechatTemplate.setUrl(kongxc_wx_diancan_orderServlet);
        Map<String, TemplateData> m = new HashMap<String, TemplateData>();
        TemplateData first = new TemplateData();
        first.setColor("#000000");
        first.setValue("亲！您的订单我们已受理，美食正有序制作中。。^_^");
        m.put("first", first);
        //商家名称
        TemplateData keyword1 = new TemplateData();
        keyword1.setColor("#000000");
        keyword1.setValue(courOrders.getOrderCode());
        m.put("keyword1", keyword1);
        //预定时间
        TemplateData keyword2 = new TemplateData();
        keyword2.setColor("#000000");
        keyword2.setValue(TimeTools.localDateTime2String(courOrders.getPayTime(), TimeTools.DAY_FORMAT));
        m.put("keyword2", keyword2);

        TemplateData remark = new TemplateData();
        remark.setColor("#000000");
        remark.setValue("感谢您的点餐!");
        m.put("remark", remark);
        wechatTemplate.setData(m);
        try {
            return sendTemplateMessage(access_token, wechatTemplate);
        } catch (Exception e) {
            log.info("异常" + e.getMessage());
        }
        return null;
    }

    //	订餐取餐通知
    public JSONObject quCanMethod(String access_token, MemberBaseInfo memberBaseInfo, CoursesOrders courOrders, String url) {
        WechatTemplate wechatTemplate = new WechatTemplate();
        wechatTemplate.setTemplate_id(Template_quCan_id);
        wechatTemplate.setTouser(memberBaseInfo.getOpenid());
        wechatTemplate.setUrl(url);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY-MM-DD");
        DateTimeFormatter formatterTime = DateTimeFormatter.ofPattern("HH:mm");
        Map<String, TemplateData> m = new HashMap<String, TemplateData>();
        TemplateData first = new TemplateData();
        first.setColor("#000000");
        first.setValue("亲！已为您备齐您的餐单，请在取餐时间范围内取餐。。^_^");
        m.put("first", first);

        //预定日期
        TemplateData keyword1 = new TemplateData();
        keyword1.setColor("#000000");
        keyword1.setValue(formatter.format(courOrders.getTakeTime()));
        m.put("keyword1", keyword1);

        //餐次
        TemplateData keyword2 = new TemplateData();
        keyword2.setColor("#000000");

        switch (courOrders.getMealCode()) {
            case "01":
                keyword2.setValue("早餐");
                break;
            case "02":
                keyword2.setValue("午餐");
                break;
            case "03":
                keyword2.setValue("晚餐");
                break;
        }
        m.put("keyword2", keyword2);

        //取餐时间
        TemplateData keyword3 = new TemplateData();
        keyword3.setColor("#000000");
        keyword3.setValue(formatterTime.format(LocalTime.now()) + " - " + formatterTime.format(LocalTime.now().plusHours(2)));
        m.put("keyword3", keyword3);
        //取餐地点
        TemplateData keyword4 = new TemplateData();
        keyword4.setColor("#000000");
        keyword4.setValue(courOrders.getAddressDetail());
        m.put("keyword4", keyword4);

        TemplateData remark = new TemplateData();
        remark.setColor("#000000");
        remark.setValue("为了保证食物新鲜美味，请您及时取餐。");
        m.put("remark", remark);
        wechatTemplate.setData(m);
        try {
            return sendTemplateMessage(access_token, wechatTemplate);
        } catch (Exception e) {
            log.info("异常" + e.getMessage());
        }
        return null;
    }


    //	项目消息推送
    public JSONObject messageSend(String access_token, MemberBaseInfo memberBaseInfo, SchoolBaseInfo school) {
        WechatTemplate wechatTemplate = new WechatTemplate();
        wechatTemplate.setTemplate_id(Template_xiaoFeiTongZhi_id);
        wechatTemplate.setTouser(memberBaseInfo.getOpenid());
        wechatTemplate.setUrl("http://music.163.com/#/song?id=27867140");
        Map<String, TemplateData> m = new HashMap<String, TemplateData>();
        TemplateData first = new TemplateData();
        first.setColor("#000000");
        first.setValue("您好!消息通知。。^_^");
        m.put("first", first);

        TemplateData remark = new TemplateData();
        remark.setColor("#000000");
        remark.setValue(school.getMessage());
        m.put("remark", remark);
        wechatTemplate.setData(m);
        try {
            return sendTemplateMessage(access_token, wechatTemplate);
        } catch (Exception e) {
            log.info("异常" + e.getMessage());
        }
        return null;
    }


    //订餐订单支付

    public JSONObject zhiFuMethod(String access_token, MemberBaseInfo memberBaseInfo,
                                  CoursesOrders orderMainInfo,
                                  List<CoursesOrdersDetails> courseDetail,
                                  String detailListStr, String kongxc_wx_diancan_orderServlet) {
        WechatTemplate wechatTemplate = new WechatTemplate();
        wechatTemplate.setTemplate_id(Template_dingDangZhifu_id);
        wechatTemplate.setTouser(memberBaseInfo.getOpenid());
        wechatTemplate.setUrl(kongxc_wx_diancan_orderServlet);
        Map<String, TemplateData> m = new HashMap<String, TemplateData>();
        TemplateData first = new TemplateData();
        first.setColor("#000000");
        first.setValue("亲！我们已收到您的订单付款，正在给您备餐中正在有序准备中。。^_^");
        m.put("first", first);
        //订单编号
        TemplateData keyword1 = new TemplateData();
        keyword1.setColor("#000000");
        keyword1.setValue(memberBaseInfo.getName());
        m.put("keyword1", keyword1);
        //订单商品
        TemplateData keyword2 = new TemplateData();
        keyword2.setColor("#000000");
        keyword2.setValue(orderMainInfo.getOrderCode());
        m.put("keyword2", keyword2);

        //订单金额
        TemplateData keyword3 = new TemplateData();
        keyword3.setColor("#000000");
        keyword3.setValue(orderMainInfo.getAmount().toString());
        m.put("keyword3", keyword3);

        //支付时间
        TemplateData keyword4 = new TemplateData();
        keyword4.setColor("#000000");
        keyword4.setValue(detailListStr);
        m.put("keyword4", keyword4);

        TemplateData remark = new TemplateData();
        remark.setColor("#000000");
        remark.setValue("感谢您的订餐!");
        m.put("remark", remark);
        wechatTemplate.setData(m);
        try {
            return sendTemplateMessage(access_token, wechatTemplate);
        } catch (Exception e) {
        }
        return null;
    }


    //-------------------------------订货------------------------


    //	订货备货通知
    public JSONObject beiHuoMethod(String access_token, MemberBaseInfo memberBaseInfo, OrderMainInfo orderMainInfo, List<OrderDetailInfo> detailList, String courseDetail, String kongxc_wx_diancan_orderServlet) {
        WechatTemplate wechatTemplate = new WechatTemplate();
        wechatTemplate.setTemplate_id(Template_jieCan_id);
        wechatTemplate.setTouser(memberBaseInfo.getOpenid());
        wechatTemplate.setUrl(kongxc_wx_diancan_orderServlet);
        Map<String, TemplateData> m = new HashMap<String, TemplateData>();
        TemplateData first = new TemplateData();
        first.setColor("#000000");
        first.setValue("亲！您的订单我们已受理，商品正在有序准备中。。^_^");
        m.put("first", first);
        //商家名称
        TemplateData keyword1 = new TemplateData();
        keyword1.setColor("#000000");
        keyword1.setValue(orderMainInfo.getCode());
        m.put("keyword1", keyword1);
        //预定时间
        TemplateData keyword2 = new TemplateData();
        keyword2.setColor("#000000");
        keyword2.setValue(TimeTools.calendar2String(orderMainInfo.getCreateTime(), TimeTools.DAY_FORMAT));
        m.put("keyword2", keyword2);


        TemplateData remark = new TemplateData();
        remark.setColor("#000000");
        remark.setValue("感谢您的订货!");
        m.put("remark", remark);
        wechatTemplate.setData(m);
        try {
            return sendTemplateMessage(access_token, wechatTemplate);
        } catch (Exception e) {
        }
        return null;
    }


    //	订货餐卡
    public JSONObject dinghuoCanKa(String access_token, MemberBaseInfo memberBaseInfo, OrderMainInfo orderMainInfo,
                                   List<OrderDetailInfo> detailList, String courseDetail, String kongxc_wx_diancan_orderServlet) {
        WechatTemplate wechatTemplate = new WechatTemplate();
        wechatTemplate.setTemplate_id(Template_id);
        wechatTemplate.setTouser(memberBaseInfo.getOpenid());
        wechatTemplate.setUrl(kongxc_wx_diancan_orderServlet);
        Map<String, TemplateData> m = new HashMap<String, TemplateData>();
        TemplateData first = new TemplateData();
        first.setColor("#000000");
        first.setValue("亲！我们已收到您(" + memberBaseInfo.getName() + ")支付的订单金额，正在精心备货中，您可在我的订单中查看。请记得关注我们呦！^_^");
        m.put("first", first);
        //订单编号
        TemplateData keyword1 = new TemplateData();
        keyword1.setColor("#000000");
        keyword1.setValue(orderMainInfo.getCode());
        m.put("keyword1", keyword1);
        //商家名称
        TemplateData keyword2 = new TemplateData();
        keyword2.setColor("#000000");
        keyword2.setValue(memberBaseInfo.getName());
        m.put("keyword2", keyword2);


        TemplateData remark = new TemplateData();
        remark.setColor("#000000");
        if (orderMainInfo.getOrderType() != null && Finals.ORDER_TYPE_SELF.equals(orderMainInfo.getOrderType())) {
            remark.setValue("感谢您的订货，请您到餐厅指定取货地点取货，欢迎您下次光临！");
        } else {
            remark.setValue("感谢您的订货，我们将会尽快将货品送达，欢迎您下次光临！");
        }

        m.put("remark", remark);
        wechatTemplate.setData(m);
        try {
            return sendTemplateMessage(access_token, wechatTemplate);
        } catch (Exception e) {
            log.info("异常" + e.getMessage());
        }
        return null;
    }


    //	微信发送方法
    public JSONObject sendTemplateMessage(String accessToken, WechatTemplate wechatTemplate) {
        String jsonString = new Gson().toJson(wechatTemplate);
        String requestUrl = Configure.SEND_TEMPLAYE_MESSAGE_URL.replace("ACCESS_TOKEN", accessToken);
        return NetTools.httpsRequest(requestUrl, "POST", jsonString);
    }


}
